1451B - Non-Substring Subsequence - CodeForces Solution


dp greedy implementation strings *900

Please click on ads to support us..

Python Code:

t=int(input())
while t:
    n,q=map(int,input().split())
    s=input()
    for i in range(q):
        l,r=map(int,input().split())
        l-=1
        r-=1
        if (s[l] in s[:l]) or (s[r] in s[r+1:]):
            print('YES')
        else:
            print('NO')
    t-=1

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <char> vc;
typedef vector <string> vs;
typedef vector <pair<int, int>> vpii;
typedef vector <pair<char, int>> vpci;
typedef pair <int, int> pii;
typedef map <int, int> mii;
typedef map <char, int> mci;
typedef map <string, int> msi;
typedef set <int> si;
typedef set <char> sc;
typedef set <string> ss;

#define endl '\n'
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define lbnd lower_bound
#define ubnd upper_bound
#define debug cerr<<"\ni'm here\n"<<endl;
#define PI 3.141592653589793

const ll MAX_N = 1000000007;

// resolvendo usando guloso:
// so sera possivel nos dois seguintes casos:
// (1) tem char igual a str[l-1] antes de str[l-1]
// (2) tem char igual a str[r-1] depois de str[r-1]

void solve(){
    int n, q, l, r;
    string str;
    cin >> n >> q >> str;
    for(int i=0; i<q; i++){
        cin >> l >> r;
        bool poss = false;
        for(int k=0; k<(l-1); k++){
            if(str[k]==str[l-1]){
                poss = true;
            }
        }
        for(int j=n-1; j>(r-1); j--){
            if(str[j]==str[r-1]){
                poss = true;
            }
        }
        if(!poss)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }

}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
        solve();
    //solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II